/**
 * Created by zhangguowei on 2015/12/11.
 */
var setting = {
    data: {
        simpleData: {
            enable: true
        }
    }, callback: {
        onClick: zTreeOnClick
    }
};
function zTreeOnClick(event, treeId, treeNode) {
    var orgName = treeNode.name;
    var orgCode = treeNode.orgCode;
    $("#orgName").val(orgName);
    $("#orgCode").val(orgCode);
    $("#linkMan").val("");
    $("#telephoneNo").val("");
    $("#indx").val("");
    $.post($basePath + "/mgn/userOrg/getOrgInfo ", {orgCode: orgCode}, function (data) {
        if (data) {
            $("#orgName").val(data.orgName);
            $("#orgCode").val(data.orgCode);
            $("#linkMan").val(data.linkMan);
            $("#telephoneNo").val(data.telephoneNo);
            $("#indx").val(data.indx);
        }
    });
    $("#corptable").jqGrid('setGridParam', {
        url: $basePath + "/mgn/userOrg/queryUserInfoList", postData: {orgCode: orgCode}, page: 1
    }).trigger("reloadGrid");
};
var userOrg = {
    initUserRoleSelectItems: function (userIndx) {
        $.post($basePath + "/mgn/userOrg/queryUserRole ", {userIndx: userIndx}, function (data) {
            var unSelectOption = "";
            var SelectOption = "";
            if (data) {
                for (var i = 0; i < data.length; i++) {
                    if ("1" == data[i].MULTIP) {
                        SelectOption += "<option value=\"" + data[i].INDX + "\">" + data[i].ROLE_NAME + "</option>"
                    } else {
                        unSelectOption += "<option value=\"" + data[i].INDX + "\">" + data[i].ROLE_NAME + "</option>"
                    }
                }
            }
            $("#keepRenderingSort").html(unSelectOption);
            $("#keepRenderingSort_to").html(SelectOption);
        });
    }, initInspOrgTreeList: function () {
        var zNodes = new Array();
        $.post($basePath + "/mgn/userOrg/queryOrg", function (data) {
            for (var i = 0; i < data.length; i++) {
                var tree = new Object();
                tree.id = data[i].indx;
                tree.orgCode = data[i].orgCode;

                if (null == data[i].parentIndx) {
                    tree.icon = $basePath + "/plugins/ztree/zTreeStyle/img/diy/1_open.png";
                    tree.open = true;
                    tree.pId = '';
                } else {
                    tree.pId = data[i].parentIndx;
                }
                tree.name = data[i].orgName;
                zNodes.push(tree);
            }
            $.fn.zTree.init($("#inspOrgTree"), setting, zNodes);
            var zTree = $.fn.zTree.getZTreeObj("inspOrgTree");
            var orgCode = $("#orgCode").val();
            if (orgCode != null && orgCode != '') {
                var node = zTree.getNodeByParam("orgCode", orgCode);
                zTree.selectNode(node);
            }

        });


    }, orgAdd: function () {

    }, orgEdit: function () {
        $.post($basePath + "/mgn/userOrg/updateOrgInfo", $("#orgForm").serialize(), function (data) {

        });
    }, orgSave: function () {
        var tel = $("#telephoneNo").val();
        if (tel != null && tel != '' && !checkTel(tel)) {
            showValidateTipsMsg("请输入有效的联系电话", $("#telephoneNo"));
            return false;
        }
        $.post($basePath + "/mgn/userOrg/saveOrgInfo", $("#orgForm").serialize(), function () {
            showOKMessage("保存成功");
        }).error(function () {
            showWarningMsg("保存失败");
        });
    }, orgDisable: function () {

    }, userAdd: function () {
        location.href = $basePath + "/mgn/userOrg/userAdd";
    }, updateUserInfo: function (gridId, status) {
        var selectedId = $("#" + gridId).jqGrid("getGridParam", "selrow");
        if (!selectedId) {
            ShowMessages("对不起,您没有选中任何记录", "系统提示", "msgwaring", null, 0);
            return;
        } else {
            var showMsg = "";
            if ('0' == status) {
                showMsg = "禁用";
            } else if ('1' == status) {
                showMsg = "启用";
            } else {
                return false;
            }
            showConfirm("确定要" + showMsg + "选中的数据吗？", "确定要" + showMsg, function () {
                var selectedRowIds = $("#" + gridId).jqGrid("getGridParam", "selarrrow");
                var len = selectedRowIds.length;
                var jsonSeqId = "";
                for (var i = 0; i < len; i++) {
                    var rowData = $("#" + gridId + "").jqGrid("getRowData", selectedRowIds[i]);
                    jsonSeqId += rowData.indx;
                    jsonSeqId += ",";
                }
                jsonSeqId = jsonSeqId.substr(0, jsonSeqId.length - 1);
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: $basePath + "/mgn/userOrg/deleteUserInfo",
                    data: {"indxs": jsonSeqId, "status": status},
                    success: function (result) {
                        if (result) {
                            showOKMessage("数据" + showMsg + "成功!", "", function () {
                                $("#" + gridId).jqGrid('setGridParam', {
                                    url: $basePath + "/mgn/userOrg/queryUserInfoList",
                                    postData: {orgCode: $("#orgCode").val()},
                                    page: 1
                                }).trigger("reloadGrid");
                            });
                        } else {
                            ShowMessages("数据" + showMsg + "失败", "系统提示", "msgwaring", null, 0);
                        }
                    }
                });
            });
        }
    }, userEdit2: function (indx) {
        $.post($basePath + "/mgn/userOrg/updateUserInfo", $("#userForm").serialize(), function (data) {

        });
    }, userRole: function () {

    }, roleUserSave: function (userIndx) {
        var roleIndx = "";
        $("#keepRenderingSort_to>option").each(function () {
            roleIndx += $(this).val() + ","
        });
        $.post($basePath + "/mgn/userOrg/saveRoleUser", {userIndx: userIndx, roleIndx: roleIndx}, function (data) {

        });
    }, getRoleUser: function () {
        var roleIndx = "";
        $("#keepRenderingSort_to>option").each(function () {
            roleIndx += $(this).val() + ","
        });
        $("input[name='selectRoleIndxs']").val(roleIndx);
        return roleIndx;
    }
};
function checkTel(phoneNo) {
    var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
    var isMob = /^((\+?86)|(\(\+86\)))?(13[0123456789][0-9]{8}|15[0123456789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
    if (isMob.test(phoneNo) || isPhone.test(phoneNo)) {
        return true;
    } else {
        return false;
    }
}

userOrg.getRoleUser();

